package com.lhkj.iams.wechat.config.mp.handler;

import com.lhkj.iams.wechat.config.mp.builder.TextBuilder;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import org.springframework.stereotype.Component;

import java.util.Map;


/**
 * @desc 订阅处理
 */
@Component
public class SubscribeHandler extends AbstractHandler {

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
        Map<String, Object> context, WxMpService weixinService,
        WxSessionManager sessionManager) throws WxErrorException {
        try {
            WxMpConfigStorage wxMpConfigStorage = weixinService.getWxMpConfigStorage();
            this.logger.info("当前公众号AppId:[{}]；新关注用户 OPENID: [{}]", wxMpConfigStorage.getAppId(),
                wxMessage.getFromUser());
            // TODO: 2023/3/27 此处可以循环调用KfSessionHandler发送消息
            //默认返回一条信息
            return new TextBuilder().build("订阅成功", wxMessage, weixinService);
        } catch (
            Exception e) {
            this.logger.error(e.getMessage(), e);
        }
        return null;
    }
}

